package com.wwwarehouse.contract.contract.info;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.adapter.InfoCardOrderDetailAdapter;
import com.wwwarehouse.contract.bean.InfoCardContractGoodsDetailBean;
import com.wwwarehouse.contract.bean.InfoCardOrderDetailBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/11/3.
 */

public class InfoCardOrderDetailFragment extends TheParentFragment {
	private RecyclerView mRecycleView;
	private InfoCardOrderDetailAdapter mOrderDetailAdapter;
	private List<InfoCardContractGoodsDetailBean.ItemDetailBean> mList = new ArrayList<>();
	@Override
	protected int getContentId() {
		return R.layout.info_card_order_detail_fragment;
	}

	@Override
	public void setTitle() {
		mActivity.setTitle(R.string.contract_modify_info_card_goods_details_title);
	}

	@Override
	protected void init(View view) {
		mRecycleView = (RecyclerView) view.findViewById(R.id.recycle_view);
		mRecycleView.setLayoutManager(new LinearLayoutManager(mActivity));

	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
				final InfoCardContractGoodsDetailBean listBean = (InfoCardContractGoodsDetailBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);

				int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				mList.clear();
				if (listBean == null) {
					return null;
				}
				for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
					if (i >= 0 && i < listBean.getList().size()) {
						mList.add(listBean.getList().get(i));
					}
				}
				mRecycleView.post(new Runnable() {
					@Override
					public void run() {
						mOrderDetailAdapter = new InfoCardOrderDetailAdapter(mList, mActivity , mRecycleView.getHeight());
						mRecycleView.setAdapter(mOrderDetailAdapter);
						mOrderDetailAdapter.setOnItemClickListener(new InfoCardOrderDetailAdapter.OnItemClickListener() {
							@Override
							public void OnItemClick(int position) {
								//跳转到商品详情
								try {
									Bundle bundle = new Bundle();
									bundle.putString("itemType", "H_SALE");
									bundle.putString("type", "0");
									bundle.putLong("ukid" , Long.valueOf(mList.get(position).getDefinedUkid()));
									bundle.putString("typeName" , "商品详细信息");
									pushFragment("/ResourceCenter/InfomationResourcesFragment" , bundle);
								} catch (NumberFormatException e) {
									e.printStackTrace();
									toast("参数有误");
								}
							}
						});
					}
				});

			}
		}
		return null;
	}

	@Override
	public void requestDatas() {
		super.requestDatas();


	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {

	}

	@Override
	public void onReLoad(int code) {

	}
}
